using System;
using EPiServer.Common;
using EPiServer.Common.Queries;
using EPiServer.Community.Calendar;
using EPiServer.Community.Calendar.Queries;

namespace EPiAbstractions.Community.Calendar
{
    public class CalendarHandlerFacade : ICalendarHandlerFacade
    {
        private static CalendarHandlerFacade _instance;

        public CalendarHandlerFacade()
        {
            CalendarHandler.EventAdded += OnEventAdded;
            CalendarHandler.EventUpdated += OnEventUpdated;
            CalendarHandler.EventRemoved += OnEventRemoved;
            CalendarHandler.CalendarAdded += OnCalendarAdded;
            CalendarHandler.CalendarUpdated += OnCalendarUpdated;
            CalendarHandler.CalendarRemoved += OnCalendarRemoved;
            CalendarHandler.InviteAdded += OnInviteAdded;
            CalendarHandler.InviteUpdated += OnInviteUpdated;
            CalendarHandler.RegistrationAdded += OnRegistrationAdded;
            CalendarHandler.RegistrationRemoved += OnRegistrationRemoved;
        }

        public static CalendarHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CalendarHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region ICalendarHandlerFacade Members

        public event EPiServerCommonEventHandler EventAdded;
        public event EPiServerCommonEventHandler EventUpdated;
        public event EPiServerCommonEventHandler EventRemoved;
        public event EPiServerCommonEventHandler CalendarAdded;
        public event EPiServerCommonEventHandler CalendarUpdated;
        public event EPiServerCommonEventHandler CalendarRemoved;
        public event EPiServerCommonEventHandler InviteAdded;
        public event EPiServerCommonEventHandler InviteUpdated;
        public event EPiServerCommonEventHandler RegistrationAdded;
        public event EPiServerCommonEventHandler RegistrationRemoved;

        public virtual EPiServer.Community.Calendar.Calendar AddCalendar(EPiServer.Community.Calendar.Calendar calendar)
        {
            return CalendarHandler.AddCalendar(calendar);
        }

        public virtual void UpdateCalendar(EPiServer.Community.Calendar.Calendar calendar)
        {
            CalendarHandler.UpdateCalendar(calendar);
        }

        public virtual void RemoveCalendar(EPiServer.Community.Calendar.Calendar calendar)
        {
            CalendarHandler.RemoveCalendar(calendar);
        }

        public virtual EPiServer.Community.Calendar.Calendar GetCalendar(Int32 id)
        {
            return CalendarHandler.GetCalendar(id);
        }

        public virtual CalendarCollection GetCalendars()
        {
            return CalendarHandler.GetCalendars();
        }

        public virtual CalendarCollection GetCalendars(SiteCollection sites)
        {
            return CalendarHandler.GetCalendars(sites);
        }

        public virtual CalendarCollection GetCalendars(SiteCollection sites, NullValueAction siteNullAction)
        {
            return CalendarHandler.GetCalendars(sites, siteNullAction);
        }

        public virtual Int32 GetNumOfEvents(EPiServer.Community.Calendar.Calendar calendar, Boolean published)
        {
            return CalendarHandler.GetNumOfEvents(calendar, published);
        }

        public virtual Event AddEvent(Event calendarEvent)
        {
            return CalendarHandler.AddEvent(calendarEvent);
        }

        public virtual void UpdateEvent(Event calendarEvent)
        {
            CalendarHandler.UpdateEvent(calendarEvent);
        }

        public virtual EventRecurrence GetEventRecurrence(Int32 eventId)
        {
            return CalendarHandler.GetEventRecurrence(eventId);
        }

        public virtual void RemoveEvent(Event calendarEvent)
        {
            CalendarHandler.RemoveEvent(calendarEvent);
        }

        public virtual Event GetEvent(Int32 id)
        {
            return CalendarHandler.GetEvent(id);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, Int32 page, Int32 pageSize,
                                                 EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, page, pageSize, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, Int32 page, Int32 pageSize,
                                                 Boolean singleOccurrencesOnly, EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, page, pageSize, singleOccurrencesOnly, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, Int32 page, Int32 pageSize,
                                                 out Int32 totalItems, EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, page, pageSize, out totalItems, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, Int32 page, Int32 pageSize,
                                                 Boolean singleOccurrencesOnly, out Int32 totalItems, EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, page, pageSize, singleOccurrencesOnly, out totalItems, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, EventPublishState publishState, Int32 page,
                                                 Int32 pageSize, EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, publishState, page, pageSize, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, EventPublishState publishState, Int32 page,
                                                 Int32 pageSize, out Int32 totalItems, EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, publishState, page, pageSize, out totalItems, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, Int32 page, Int32 pageSize,
                                                 DateTime fromDate, DateTime toDate, EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, page, pageSize, fromDate, toDate, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, Int32 page, Int32 pageSize,
                                                 DateTime fromDate, DateTime toDate, out Int32 totalItems, EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, page, pageSize, fromDate, toDate, out totalItems, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, EventPublishState publishState, Int32 page,
                                                 Int32 pageSize, DateTime fromDate, DateTime toDate, EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, publishState, page, pageSize, fromDate, toDate, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, EventPublishState publishState, Int32 page,
                                                 Int32 pageSize, DateTime fromDate, DateTime toDate, out Int32 totalItems,
                                                 EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, publishState, page, pageSize, fromDate, toDate, out totalItems, sortOrder);
        }

        public virtual EventCollection GetEvents(EPiServer.Community.Calendar.Calendar calendar, EventPublishState publishState, Int32 page,
                                                 Int32 pageSize, DateTime fromDate, DateTime toDate, Boolean singleOccurrencesOnly,
                                                 out Int32 totalItems, EventSortOrder[] sortOrder)
        {
            return CalendarHandler.GetEvents(calendar, publishState, page, pageSize, fromDate, toDate, singleOccurrencesOnly, out totalItems,
                                             sortOrder);
        }

        public virtual Invite AddInvite(Invite invite)
        {
            return CalendarHandler.AddInvite(invite);
        }

        public virtual Invite GetInvite(Int32 id)
        {
            return CalendarHandler.GetInvite(id);
        }

        public virtual Invite GetInvite(Event calendarEvent, IAuthor invitee)
        {
            return CalendarHandler.GetInvite(calendarEvent, invitee);
        }

        public virtual InviteCollection GetInvites(Event calendarEvent, Int32 page, Int32 pageSize, InviteSortOrder[] sortOrder)
        {
            return CalendarHandler.GetInvites(calendarEvent, page, pageSize, sortOrder);
        }

        public virtual InviteCollection GetInvites(Event calendarEvent, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                   InviteSortOrder[] sortOrder)
        {
            return CalendarHandler.GetInvites(calendarEvent, page, pageSize, out totalItems, sortOrder);
        }

        public virtual InviteCollection GetInvites(Event calendarEvent, RegistrationStatus registrationStatus, Int32 page, Int32 pageSize,
                                                   InviteSortOrder[] sortOrder)
        {
            return CalendarHandler.GetInvites(calendarEvent, registrationStatus, page, pageSize, sortOrder);
        }

        public virtual InviteCollection GetInvites(Event calendarEvent, RegistrationStatus registrationStatus, Int32 page, Int32 pageSize,
                                                   out Int32 totalItems, InviteSortOrder[] sortOrder)
        {
            return CalendarHandler.GetInvites(calendarEvent, registrationStatus, page, pageSize, out totalItems, sortOrder);
        }

        public virtual void UpdateInvite(Invite invite)
        {
            CalendarHandler.UpdateInvite(invite);
        }

        public virtual Registration AddRegistration(Registration registration)
        {
            return CalendarHandler.AddRegistration(registration);
        }

        public virtual Registration GetRegistration(Int32 id)
        {
            return CalendarHandler.GetRegistration(id);
        }

        public virtual Registration GetRegistration(Event calendarEvent, IAuthor registrant)
        {
            return CalendarHandler.GetRegistration(calendarEvent, registrant);
        }

        public virtual RegistrationCollection GetRegistrations(Event calendarEvent, Int32 page, Int32 pageSize,
                                                               RegistrationSortOrder[] sortOrder)
        {
            return CalendarHandler.GetRegistrations(calendarEvent, page, pageSize, sortOrder);
        }

        public virtual RegistrationCollection GetRegistrations(Event calendarEvent, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                               RegistrationSortOrder[] sortOrder)
        {
            return CalendarHandler.GetRegistrations(calendarEvent, page, pageSize, out totalItems, sortOrder);
        }

        public virtual void RemoveRegistration(Registration registration)
        {
            CalendarHandler.RemoveRegistration(registration);
        }

        public virtual CalendarCollection GetQueryResult(CalendarQuery query)
        {
            return CalendarHandler.GetQueryResult(query);
        }

        public virtual CalendarCollection GetQueryResult(CalendarQuery query, Int32 page, Int32 pageSize)
        {
            return CalendarHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual CalendarCollection GetQueryResult(CalendarQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return CalendarHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual EventCollection GetQueryResult(EventQuery query)
        {
            return CalendarHandler.GetQueryResult(query);
        }

        public virtual EventCollection GetQueryResult(EventQuery query, Int32 page, Int32 pageSize)
        {
            return CalendarHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual EventCollection GetQueryResult(EventQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return CalendarHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual InviteCollection GetQueryResult(InviteQuery query)
        {
            return CalendarHandler.GetQueryResult(query);
        }

        public virtual InviteCollection GetQueryResult(InviteQuery query, Int32 page, Int32 pageSize)
        {
            return CalendarHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual InviteCollection GetQueryResult(InviteQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return CalendarHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual RegistrationCollection GetQueryResult(RegistrationQuery query)
        {
            return CalendarHandler.GetQueryResult(query);
        }

        public virtual RegistrationCollection GetQueryResult(RegistrationQuery query, Int32 page, Int32 pageSize)
        {
            return CalendarHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual RegistrationCollection GetQueryResult(RegistrationQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return CalendarHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        #endregion

        public virtual void OnEventAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (EventAdded != null)
                EventAdded(sender, e);
        }

        public virtual void OnEventUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (EventUpdated != null)
                EventUpdated(sender, e);
        }

        public virtual void OnEventRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (EventRemoved != null)
                EventRemoved(sender, e);
        }

        public virtual void OnCalendarAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (CalendarAdded != null)
                CalendarAdded(sender, e);
        }

        public virtual void OnCalendarUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (CalendarUpdated != null)
                CalendarUpdated(sender, e);
        }

        public virtual void OnCalendarRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (CalendarRemoved != null)
                CalendarRemoved(sender, e);
        }

        public virtual void OnInviteAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (InviteAdded != null)
                InviteAdded(sender, e);
        }

        public virtual void OnInviteUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (InviteUpdated != null)
                InviteUpdated(sender, e);
        }

        public virtual void OnRegistrationAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (RegistrationAdded != null)
                RegistrationAdded(sender, e);
        }

        public virtual void OnRegistrationRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (RegistrationRemoved != null)
                RegistrationRemoved(sender, e);
        }
    }
}